/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hearts;

import java.util.ArrayList;

/**
 *
 * @author TAI
 */
abstract public class player {
	protected String ten;
	protected card[] danhSachCard;
	protected Boolean[] cardDaChoi;
	protected ArrayList<card> scoreCard;
	protected int score;

	public player(){
		ten="";
		score=0;
		danhSachCard=new card[14];
		cardDaChoi=new Boolean[14];
		scoreCard=new ArrayList<card>();
	}

	public void setTen(String ten){
		this.ten=ten;
	}

	public String getTen(){
		return ten;
	}

	public void setScore(int score){
		this.score=score;
	}

	public int getScore(){
		return score;
	}

	public void addScore(int score){
		this.score+=score;
	}

	public boolean kiemTraCardChuaChoi(int index){
		return !(cardDaChoi[index]);
	}

	public void nhanCard(card c){
		for (int i=1;i<=13;i++)
			if (danhSachCard[i]==null){
				danhSachCard[i]=c;
				break;
			}
	}

	public void nhanCard(int index,card c){
		danhSachCard[index]=c;
	}

	public card layCardTrongDanhSach(int index){
		if (index<14)
			return danhSachCard[index];
		return null;
	}

	public card passLeftMotCard(int index){
		card tam=danhSachCard[index];
		danhSachCard[index]=null;
		return tam;
	}

	public void sapXepCardTrongDanhSach(){
		card[] danhSachCard2=new card[14];
		card[] danhSachCardTheoChat=new card[14];
		int tro1=0,tro2;
		for (int chatCard=1;chatCard<=4;chatCard++){
			if (chatCard==4) chatCard=0;
			tro2=0;
			//lay danh sach Card theo chat 
			for (int i=1;i<=13;i++){
				if (danhSachCard[i].getChat()==chatCard){
					tro2++;
					danhSachCardTheoChat[tro2]=danhSachCard[i];
				}
			}
			//sap xep danh sach Card theo chat do
			for (int i=1;i<tro2;i++)
				for (int j=i;j<=tro2;j++){
					if (danhSachCardTheoChat[j].getID()<danhSachCardTheoChat[i].getID()){
						card cardtam=danhSachCardTheoChat[i];
						danhSachCardTheoChat[i]=danhSachCardTheoChat[j];
						danhSachCardTheoChat[j]=cardtam;
					}
				}

			//dua vao danhSachCard2
			for (int i=1;i<=tro2;i++){
				tro1++;
				danhSachCard2[tro1]=danhSachCardTheoChat[i];
			}
			if (chatCard==0) chatCard=4;
		}
		danhSachCard=danhSachCard2;
	}

	public boolean kiemTraDaChuaCard(int cardID){
		for (int i=1;i<=13;i++){
			if (danhSachCard[i].getID()==cardID) return true;
		}
		return false;
	}

	public void add4ScoreCard(ArrayList<card> bonCard){
		for (int i=0;i<=3;i++){
			scoreCard.add(bonCard.get(i));
			if (bonCard.get(i).kiemTraChatCo()) addScore(1);
                        //idCard = 41 la id cua con dam bich
			if (bonCard.get(i).getID()==41) addScore(13);
		}
		bonCard.clear();
	}

        //Bat dau 1 hiep moi
	public void newRound(){
		for (int i=1;i<=13;i++){
			danhSachCard[i]=null;
			cardDaChoi[i]=false;
		}
		scoreCard.clear();
	}

	public void resetScore(){
		score=0;
	}

	public boolean kiemTraShootTheMoon(){
		int demCo=0;
		for (int i=0;i<scoreCard.size();i++){
			if (scoreCard.get(i).kiemTraChatCo()) demCo++;
		}
		if (demCo==13){
			for (int i=0;i<scoreCard.size();i++)
				if (scoreCard.get(i).getID()==41){
					addScore(-26);
					return true;
				}
			return false;
		}
		else
			return false;
	}
}

